package org.example.config;


import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


@Configuration
public class RabbitmqOneConfig {

    public static final String QUEUE_NAME="work-queue";

    public static final String RUN_CASE_QUEUE_NAME="case-queue";

    @Value("${spring.rabbitmq.one.host}")
    private String host;

    @Value("${spring.rabbitmq.one.port}")
    private int port;

    @Value("${spring.rabbitmq.one.username}")
    private String username;

    @Value("${spring.rabbitmq.one.password}")
    private String password;

    @Value("${spring.rabbitmq.one.virtual-host}")
    private String virtualhost;

    @Bean(name = "oneConnectionFactory")
    @Primary
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory();
        factory.setHost(host);
        factory.setPort(port);
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setVirtualHost(virtualhost);
        //connectionFactory.setPublisherConfirms(true);
        return factory;
    }
    @Bean(name = "oneRabbitAdmin")
    @Primary
    public RabbitAdmin rabbitAdmin(@Qualifier("oneConnectionFactory") ConnectionFactory factory) {
        RabbitAdmin admin = new RabbitAdmin(factory);
        admin.setAutoStartup(true);
        return admin;
    }

    @Bean(name = "oneRabbitTemplate")
    @Primary
    public RabbitTemplate rabbitTemplate(@Qualifier("oneConnectionFactory") ConnectionFactory factory) {
        RabbitTemplate template =new RabbitTemplate(factory);
        // setMessageConverter 方法使用jackson2去实现序列化
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        return template;
    }



    @Bean(name = "oneFactory")
    @Primary
    public SimpleRabbitListenerContainerFactory twoFactory(@Qualifier("oneConnectionFactory") ConnectionFactory connectionFactory,
                                                           SimpleRabbitListenerContainerFactoryConfigurer configurer) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }



    /**
     * 声明提交代码队列名称
     * @return
     */
    @Bean
    public Queue createQueueSubmit(){
        return new Queue(QUEUE_NAME);
    }



    /**
     * 声明提交样例队列名称
     * @return
     */
    @Bean
    public Queue createQueueRun(){
        return new Queue(RUN_CASE_QUEUE_NAME);
    }

}